<?php
namespace app\generate\controller;

use mysql_xdevapi\DocResult;
use think\Db;
use think\facade\Config;
use app\common\controller\Base;
use think\facade\Build;
use think\facade\Env;
/**
 * @title vue代码生成(05-02)
 * @description vue代码生成
 * @group 代码生成(05)
 * @param name:token type:int require:1 default:1 other: desc:token
 */
class CreateVue extends Base
{
    /**
     * @title 创建
     * @description 创建api、首页、添加、编辑
     * @author 开发者（宋晓文）
     * @url /generate/create_vue/build
     * @method POST
     * @param name:table_id type:int require:1 default:0 other: desc:数据表ID
     * @param name:field type:array require:1 default:0 other: desc:字段信息
     *
     * @return:
     */
    public function build(){
        $input=input();

        $rule = [
            'table_id'  =>[ 'require','number'],
            'field'  =>[ 'require'],
        ];
        $vmsg = [
            'require' => '不能为空',
            'number' => '不为数字'
        ];
        $validate = new \think\Validate($rule,$vmsg);
        $result  = $validate->batch()->check($input);
        if ($result !== true) {
            $msg['msg']='校验失败';
            $msg['validate']=$validate->getError();
            $msg['error_id']='1-05-01-0001';
            return $this->errorJson($msg);
        }
        $table = Db::table('zb_generate.tp_table')->find($input['table_id']);
        $model = Db::table('zb_generate.tp_model')->find($table['model_id']);
        if (!$table || !$model){
            $msg['msg']='数据库数据获取失败';
            return $this->errorJson($msg);
        }

        $data = [];
        $data['model_name'] = $model['db_name'];
        $data['model_title'] = $model['model_name'];
        $data['table_name'] = $table['table_name'];
        $data['table_title'] = $table['table_title'];
        for($i=0;$i<count($input['field']);$i++){
            if (empty($input['field'][$i]['enum'])){
                $input['field'][$i]['enum']=[];
            }
        }
        $data['field'] = $input['field'];

        $this->basePath=Env::get('root_path');

        //生成vue/src/api/api.js
        $api_file=$this->basePath.'vue/src/api/'.parse_name($data['model_name']).'/'.lcfirst(parse_name($data['table_name'])).'.js';
        $conten=$this->create_api($data);
        $this->buildFile(array($api_file));
        file_put_contents($api_file,$conten);

        //生成vue/src/view/index.vue
        $index_file=$this->basePath.'vue/src/views/'.parse_name($data['model_name']).'/'.parse_name($data['table_name']).'/index.vue';
        $conten=$this->create_index($data);
        $this->buildFile(array($index_file));
        file_put_contents($index_file,$conten);

        //生成vue/src/view/editPage.vue
        $edit_page_file=$this->basePath.'vue/src/views/'.parse_name($data['model_name']).'/'.parse_name($data['table_name']).'/editPage.vue';
        $conten=$this->create_edit_page($data);
        $this->buildFile(array($edit_page_file));
        file_put_contents($edit_page_file,$conten);

        //生成vue/src/view/addPage.vue
        $add_page_file=$this->basePath.'vue/src/views/'.parse_name($data['model_name']).'/'.parse_name($data['table_name']).'/addPage.vue';
        $conten=$this->create_add_page($data);
        $this->buildFile(array($add_page_file));
        file_put_contents($add_page_file,$conten);

        //生成内容展示弹窗
        foreach ($data['field'] as $k=>$v){
            if($v['input_type']=='text'){
                $temp = array();
                $temp['name'] = $v['name'];
                $temp['comment'] = $v['comment'];
                $edit_page_file=$this->basePath.'vue/src/views/'.parse_name($data['model_name']).'/'.parse_name($data['table_name']).'/view'.parse_name($v['name'],true).'.vue';
                $conten=$this->create_view_text_page($temp);
                $this->buildFile(array($edit_page_file));
                file_put_contents($edit_page_file,$conten);
            }
        }


        $msg['msg']='生成成功';
        return $this->successJson($msg);
    }

    public function create_api($data){
        $this->assign($data);
        // 改变当前操作的模板路径
        $path=Env::get('module_path').'view\template\vue\api\api.js';
        return $this->fetch($path);
    }
    public function create_index($data){
        $this->assign($data);
        // 改变当前操作的模板路径
        $path=Env::get('module_path').'view\template\vue\views\index.vue';
        return $this->fetch($path);
    }
    public function create_add_page($data){
        $this->assign($data);
        // 改变当前操作的模板路径
        $path=Env::get('module_path').'view\template\vue\views\addPage.vue';
        return $this->fetch($path);
    }
    public function create_edit_page($data){
        $this->assign($data);
        // 改变当前操作的模板路径
        $path=Env::get('module_path').'view\template\vue\views\editPage.vue';
        return $this->fetch($path);
    }
    public function create_view_text_page($data){
        $this->assign($data);
        // 改变当前操作的模板路径
        $path=Env::get('module_path').'view\template\vue\views\viewText.vue';
        return $this->fetch($path);
    }
    /**
     * 创建目录
     * @access protected
     * @param  array $list 目录列表
     * @return void
     */
    protected function buildDir($list)
    {
        foreach ($list as $dir) {
            $this->checkDirBuild($this->basePath . $dir);
        }
    }
    /**
     * 创建目录
     * @access protected
     * @param  string $dirname 目录名称
     * @return void
     */
    protected function checkDirBuild($dirname)
    {
        if (!is_dir($dirname)) {
            mkdir($dirname, 0755, true);
        }
    }
    /**
     * 创建文件
     * @access protected
     * @param  array $list 文件列表
     * @return void
     */
    protected function buildFile($list)
    {

        foreach ($list as $file) {
            if (!is_dir(dirname($file))) {
                // 创建目录
                mkdir( dirname($file), 0755, true);
            }

            if (!is_file( $file)) {
                file_put_contents( $file, 'php' == pathinfo($file, PATHINFO_EXTENSION) ? "<?php\n" : '');
            }
        }
    }

}